package com.karumi.dexter.listener;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.app.Context;
import ohos.utils.IntentConstants;

/**
 * 跳转到system setting dialog监听
 */
public class SettingsClickListener implements DexClickListener {
    private Context context;

    public SettingsClickListener(Context context) {
        this.context = context;
    }

    /**
     * 权限设置页跳转请求码
     */
    public static final int REQUEST_CODE = 1024;

    @Override
    public void onClick(Component component) {
        Intent intent = new Intent();
        Intent.OperationBuilder builder = new Intent.OperationBuilder();
        builder.withAction(IntentConstants.ACTION_MANAGE_APPLICATIONS_SETTINGS);
        intent.setOperation(builder.build());
        intent.addFlags(Intent.FLAG_ABILITY_CONTINUATION);
        if (context instanceof Ability) {
            ((Ability) context).startAbility(intent);
        } else if (context instanceof AbilitySlice) {
            ((AbilitySlice) context).startAbility(intent);
        } else {
            context.startAbility(intent, REQUEST_CODE);
        }
    }
}
